package com.sinopec.yelofixedreader.stock.inn.view

import android.os.Bundle
import android.view.Gravity
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import com.alibaba.android.arouter.facade.annotation.Route
import com.jakewharton.rxbinding3.view.clicks
import com.sinopec.basemodule.BaseApplication
import com.sinopec.basemodule.base.CustomToolbar
import com.sinopec.basemodule.base.basekt.BaseActivityKt
import com.sinopec.basemodule.dialog.CommonTipsPopupWindow
import com.sinopec.basemodule.model.StockInListModel
import com.sinopec.basemodule.utils.ARouterManagerUtils
import com.sinopec.yelofixedreader.stock.inn.R
import com.sinopec.yelofixedreader.stock.inn.contract.StockInDetailContract
import com.sinopec.yelofixedreader.stock.inn.presenter.StockInDetailPresenter
import com.sinopec.yelofixedreader.stock.inn.resultcontract.RFIDIn
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_stock_in_detail.*
import java.util.concurrent.TimeUnit

/**
 * 入库详情
 */
@Route(path = ARouterManagerUtils.STOCK_IN_DETAIL_ACTIVITY)
class StockInDetailActivity :
    BaseActivityKt<StockInDetailPresenter>(layoutId = R.layout.activity_stock_in_detail),
    StockInDetailContract.View {

    private var launcherInScan: ActivityResultLauncher<RFIDIn.Params>? = null

    private var commonTipsPopupWindow: CommonTipsPopupWindow? = null

    private val mStockInListModel by lazy {
        intent.getParcelableExtra<StockInListModel>("stockInListModel")
            ?: throw IllegalArgumentException("未传递StockInListModel")
    }

    override fun configToolbar(rToolbar: CustomToolbar?) {
        super.configToolbar(rToolbar)
        rToolbar?.setToolbarVisible(false)
    }

    override fun initView(savedInstanceState: Bundle?) {
        mPresenter = StockInDetailPresenter(this)
        val userInfo = BaseApplication.getInstance().userManager.user
        //设备id
        tv_device_id.text = userInfo.deviceId
        //仓库
        tv_warehouse.text = userInfo.stockWarehouseName
        //登录用户
        tv_name.text = userInfo.userAccount

        //任务ID
        tv_taskRecordId.text = mStockInListModel.taskRecordId?.toString()
        //任务单号
        tv_taskNo.text = mStockInListModel.taskNo?.ifEmpty { "--" }
        //仓库名称-库位名称
        tv_locationName.text = mStockInListModel.locationName?.ifEmpty { "--" }
        //物料名称
        tv_mtrlName.text = mStockInListModel.mtrlName?.ifEmpty { "--" }
        //机器码
        tv_machineCode.text = mStockInListModel.machineCode?.ifEmpty { "--" }
        //预计总重量（吨）
        tv_expectedPalletQuantity.text = mStockInListModel.expectedPalletQuantity.toString()
        //任务优先级
        tv_taskPriority.text = mStockInListModel.taskPriority.toString()
    }

    override fun initEvent() {
        super.initEvent()

        launcherInScan = registerForActivityResult(RFIDIn) {
            //扫码结果
            if (it?.codeResult == RFIDIn.Result.CODE_FINISH_AND_OPEN_DETAIL) {
            }
        }


        /**
         *  确认出库完成
         */
        addDisposable(tv_confirm.clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                showCommonTipsPopupWindow(R.drawable.icon_reject,
                    "温馨提示",
                    "是否提前完成订单？",
                    "确定",
                    "取消")
            }
        )

        /**
         *  返回
         */
        addDisposable(ll_back.clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                finish()
            }
        )

        /**
         *  扫码出库
         */
        addDisposable(ll_stock_out.clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                launcherInScan?.launch(
                    RFIDIn.Params(
                        mStockInListModel
                    )
                )
            }
        )
    }

    /**
     * @author lwj
     * @Description: 提示弹窗两个按钮
     */
    private fun showCommonTipsPopupWindow(
        resId: Int,
        title: String,
        content: String,
        confirmText: String,
        cancelText: String,
    ) {
        commonTipsPopupWindow = CommonTipsPopupWindow.create(mContext,
            resId,
            title,
            content,
            confirmText,
            cancelText, object : CommonTipsPopupWindow.OnSelectListener {
                override fun onConfirm() {
                    mStockInListModel.taskRecordId?.let {
                        mPresenter.confirmStockInOrderFinish(it)
                    }
                    commonTipsPopupWindow!!.dismiss()
                }

                override fun onCancel() {
                    commonTipsPopupWindow!!.dismiss()
                }
            }).setAnchorView(mContext.rootView)
            .apply()
        commonTipsPopupWindow?.showAtLocation(mContext.rootView, Gravity.CENTER, 0, 0)
    }
}